package com.rockwell.robot2015.model.detectors;

import org.opencv.core.Point;
import org.opencv.core.Rect;

import com.rockwell.robot2015.model.ColorBlobInfo;
import com.rockwell.robot2015.model.OnColorBlobDetectListener;

public abstract class ObjectDetector implements IDetector {
	protected Point m_center;
	protected float m_radius;
	protected Rect m_boundingRect;
	
	protected boolean m_present = false;
	protected int m_width;
	protected int m_height;
	
	protected OnColorBlobDetectListener m_listener;
	
	public ObjectDetector(OnColorBlobDetectListener listener) {
		m_listener = listener;
	}
	
	@Override 
	public void setEnabled(boolean enabled) {
		m_listener.setEnabled(enabled);
	}
	
	@Override
	public boolean isPresent() {
		return m_present;
	}

	@Override
	public Point getCenter() {
		return m_center;
	}

	@Override
	public int getRadius() {
		return (int)m_radius;
	}
	
	@Override
	public Point getDrift() {
		if (m_present && m_center != null) {
			double x = m_width - m_center.x;
			double y = m_center.y - m_height / 2;
			
			return new Point(x, y);
		}
		else {
			return new Point(100, 1);
		}
	}
	
	@Override
	public Rect getBoundingRect() {
		return m_boundingRect;
	}

	@Override
	public int getWidth() {
		return m_width;
	}

	@Override
	public int getHeight() {
		return m_height;
	}

	@Override
	public void onDetect(boolean present, ColorBlobInfo info, int length,
			int width) {
		m_present = present;
		m_height = length;
		m_width = width;
		m_boundingRect = info.boundingRect;
		m_center = info.center;
		m_radius = info.radius;
	}

	@Override
	public boolean detectLine() {
		return false;
	}

	@Override
	public Point[] getLine() {
		return null;
	}

	@Override
	public void reset() {
		m_center = null;
		m_radius = 0;
		m_boundingRect = null;
		
		m_present = false;
	}
}
